package com.base.system.managedBean;

import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.Position;
import com.base.organization.service.IPositionDAO;

@ManagedBean(name = "system_listPositionBean", scope = ManagedBeanScope.REQUEST)
public class ListPositionBean {
	private final static Log logger = LogFactory.getLog(ListPositionBean.class);
	private IPositionDAO postDao = SpringBeanHolder.getBean("positionDAO");
	@Inject
	private FacesContext context;
	@Bind(id = "postGrid", attribute = "binding")
	private UIDataGrid postGrid;
	@SaveState
	@Bind(id = "positionName", attribute = "value")
	private String positionName;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			// 取所有岗位
		}
	}

	@DataModel(id = "postGrid")
	private List<Position> getPostGrid() {
		String hql = "From " + Position.class.getName();
		if (positionName != null && !"".equals(positionName)) {
			hql += " where name like '%" + positionName + "%' ";
		}
		hql += " order by name";
		return postDao.findByHql(hql);
	}

	@Action(id = "editPost")
	public void editPost() {
		Object[] objs = postGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return;
		}
		Position post = (Position) objs[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/system/editPositon.faces?id="
				+ post.getUnid();
		String script = "addCenterLayoutTab('editPositionTab','编辑岗位','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	@Action(id = "search")
	public void search() {
		postGrid.reload();
	}

	@Action(id = "reset")
	public void reset() {
		positionName = null;
		postGrid.reload();
	}

}
